package org.example.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/chatClient")
public class ChatClientController {
    /*    //不能直接注入
        ChatClient chatClient;
        //需要使用构造方法来来注入
        public ChatClientController(ChatClient.Builder chatClientBuilder) {
            this.chatClient = chatClientBuilder.defaultSystem("你叫小特, 是小a公式旗下的一款ai产品").build();
        }*/
    @Autowired
    ChatClient chatClient;

    @GetMapping("/chat")
    public String chat(String inputMessage) {
        return chatClient.prompt()
                .user(inputMessage)
                .call()
                .content();
    }

    //结构化输出
    record recipe(String name, List<String> ingredients) {
    }//新刑类定义

    @GetMapping("/chat2")
    public String recipe(String inputMessage) {
        return chatClient.prompt()
                .user(inputMessage)
                .call()
                .entity(recipe.class).toString();
    }

    /// /steam流式输出
    @GetMapping(value = "/chat3",produces = "text/html; charset=utf-8")
    public Flux<String> recipe2(String inputMessage) {
        return chatClient.prompt()
                .advisors(new SimpleLoggerAdvisor())
                .user(inputMessage)
                .stream()//steam流式输出
                .content();
    }

}
